package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.RegistBean;
import com.ln.lnzw.net.HttpMethods;
import com.ln.lnzw.utils.CacheActivity;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.utils.TokenUtils;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wuwei on 2018/4/8.
 * 自然人设置密码
 */

public class NaturalPersonRegistrationActivity extends BaseActivity {
    @BindView(R.id.rl_registered)
    RelativeLayout rlRegistered;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.cet_mima_one)
    ClearableEditText cetMimaOne;
    @BindView(R.id.cet_mima_two)
    ClearableEditText cetMimaTwo;
    @BindView(R.id.iv_duihao)
    ImageView ivDuihao;
    @BindView(R.id.rl_tongyi)
    RelativeLayout rlTongyi;
    @BindView(R.id.cet_youxiang)
    ClearableEditText cetYouxiang;
    @BindView(R.id.tv_tongyi)
    TextView tvTongyi;

    private String phone;
    private String yanzhengma;
    private boolean flag = true;
    private String regWay="1";//1自然人2法人
    private String regType="1";//1手机，2邮箱，不填为账号
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_natural_person_registration);
        ButterKnife.bind(this);
        CacheActivity.addActivity(NaturalPersonRegistrationActivity.this);
        phone = getIntent().getExtras().getString("phone");
        yanzhengma = getIntent().getExtras().getString("yanzhengma");
        setView();





    }

    private void initData() {
        HttpMethods.getInstanceCenter().info.regist(TokenUtils.getInstance().GetGUID(),phone,cetMimaOne.getText().toString(),regWay,regType,cetYouxiang.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RegistBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(RegistBean registBean) {
                        Log.i("555", "onNext: "+registBean.toString());
                        if ("200".equals(registBean.getCode())) {
                              ToastFactory.getToast(activity,registBean.getMsg()).show();
                            ActivityUtils.startActivity(activity,VerifiedActivity.class);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("555", "onNext: "+e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });



    }

    private void setView() {
        cetMimaOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (flag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });
        cetMimaTwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (flag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });
        cetYouxiang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (flag) {//未选中状态
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                }
            }
        });

    }

    @OnClick({R.id.iv_back, R.id.rl_registered, R.id.rl_tongyi,R.id.tv_tongyi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_registered:
                if (flag) {

                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        //判断俩次密码是否一致
                        if (!cetMimaOne.getText().toString().equals(cetMimaTwo.getText().toString())) {
                            ToastFactory.getToast(this, "密码不一致").show();
                            return;
                        }
                        //判断邮箱格式
                        if (!RegexUtils.isEmail(cetYouxiang.getText())) {
                            ToastFactory.getToast(this, "邮箱格式错误").show();
                            return;
                        }
//                        ActivityUtils.startActivity(activity, VerifiedActivity.class);
                        initData();
                    }
                }
                break;
            case R.id.rl_tongyi:
                if (flag) {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_yes);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_yes);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    flag = false;
                } else {
                    if (!TextUtils.isEmpty(cetMimaOne.getText()) && !TextUtils.isEmpty(cetMimaTwo.getText()) && !TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_no);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    if (TextUtils.isEmpty(cetMimaOne.getText()) || TextUtils.isEmpty(cetMimaTwo.getText()) || TextUtils.isEmpty(cetYouxiang.getText())) {
                        ivDuihao.setImageResource(R.mipmap.icon_zhuche_tongyi_no);
                        rlRegistered.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
                    }
                    flag = true;
                }
                break;


            case R.id.tv_tongyi:
                Bundle b = new Bundle();
                b.putString("webUrl","file:///android_asset/LNnet.html");
                b.putString("name","辽宁政府服务APP用户协议");
                ActivityUtils.startActivity(b,activity, UserAgreementActivity.class);
                break;
        }
    }



}
